//
//  PGIndexBannerSubiew.m
//  NewPagedFlowViewDemo
//
//  Created by Mars on 16/6/18.
//  Copyright © 2016年 Mars. All rights reserved.
//  Designed By PageGuo,
//  QQ:799573715
//  github:https://github.com/PageGuo/NewPagedFlowView

#import "PGIndexBannerSubiew.h"

@implementation PGIndexBannerSubiew

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.layer.shadowColor=[UIColor grayColor].CGColor;
        self.layer.shadowOffset=CGSizeMake(1, 6);
        self.layer.shadowOpacity=0.5;
        self.layer.shadowRadius=8;
        [self addSubview:self.mainImageView];
        [self addSubview:self.coverView];
    }
    
    return self;
}

- (UIImageView *)mainImageView {
    
    if (_mainImageView == nil) {
        _mainImageView = [[UIImageView alloc] init];
        _mainImageView.contentMode = UIViewContentModeScaleToFill;
        _mainImageView.userInteractionEnabled = YES;
        _mainImageView.frame = CGRectMake(0, 0, KScreenWidth-60, KScreenHeight*0.55);

        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_mainImageView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _mainImageView.bounds;
        maskLayer.path = maskPath.CGPath;
        _mainImageView.layer.mask = maskLayer;

        
        self.deteleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.deteleBtn addTarget:self action:@selector(removeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        self.deteleBtn.frame = CGRectMake(15, 15, 40, 50);
        [self.deteleBtn setImage:[UIImage imageNamed:@"p_center_dtxq_delete_button"] forState:UIControlStateNormal];
        
        [_mainImageView addSubview:self.deteleBtn];
        
        self.messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.messageBtn addTarget:self action:@selector(messageBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        self.messageBtn.frame = CGRectMake(_mainImageView.width-45, 15, 40, 50);
        [self.messageBtn setImage:[UIImage imageNamed:@"viedo_message_icon"] forState:UIControlStateNormal];
        
        [_mainImageView addSubview:self.messageBtn];
        
    }
    return _mainImageView;
}



- (UIView *)coverView {
    if (_coverView == nil) {
        _coverView = [[UIView alloc] init];
        _coverView.frame = CGRectMake(0, _mainImageView.bottom, KScreenWidth-60, self.height - _mainImageView.height-60);
        _coverView.backgroundColor = [UIColor whiteColor];
        
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_coverView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _coverView.bounds;
        maskLayer.path = maskPath.CGPath;
        _coverView.layer.mask = maskLayer;
        
        
        _nameLab = [[UILabel alloc] init];
        _nameLab.font = FontP(20);
        _nameLab.textColor = [UIColor blackColor];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        [_coverView addSubview:_nameLab];
        [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_coverView).offset(15);
            make.top.equalTo(_coverView).offset(15);
            make.height.mas_equalTo(30*SCREEN_WIDTH_RATIO47);
        }];
        
        _ageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.ageBtn.enabled = NO;
        self.ageBtn.layer.cornerRadius = 4;
        self.ageBtn.layer.masksToBounds = YES;
        [self.ageBtn setTitleColor:Theme_Color_White forState:UIControlStateNormal];
        self.ageBtn .imageEdgeInsets = UIEdgeInsetsMake(0,-5,0,0);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
        self.ageBtn.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
        [_coverView addSubview:_ageBtn];
        
        [_ageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_nameLab.mas_right).offset(10);
            make.top.equalTo(_coverView).offset(17);
            make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
            make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
            
        }];
        
        _constellationLab = [[UILabel alloc] init];
        _constellationLab.textColor = Theme_Color_White;
        _constellationLab.layer.cornerRadius = 4;
        _constellationLab.layer.masksToBounds = YES;
        _constellationLab.backgroundColor = RandomColor;
        _constellationLab.textAlignment = NSTextAlignmentCenter;
        [_coverView addSubview:_constellationLab];
        
        [_constellationLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_ageBtn.mas_right).offset(5);
            make.top.equalTo(_coverView).offset(17);
            
            make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
            make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
            
        }];
        self.constellationLab.font = FontP(12);
        self.ageBtn.titleLabel.font = FontBold(12);
        
        _addressImg =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder_date"]];
        [_coverView addSubview:_addressImg];
        
        [_addressImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_coverView).offset(15);
            make.top.equalTo(_nameLab.mas_bottom).offset(10);
            make.height.mas_equalTo(15*SCREEN_WIDTH_RATIO47);
            make.width.mas_equalTo(15*SCREEN_WIDTH_RATIO47);
        }];
        
        _address = [[UILabel alloc] init];
        _address.textAlignment = NSTextAlignmentLeft;
        _address.textColor = RGB(110, 110, 110);
        _address.font = FontP(13);
        [_coverView addSubview:_address];
        [_address mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_addressImg.mas_right).offset(5);
            make.top.equalTo(_nameLab.mas_bottom).offset(5);
            make.height.mas_equalTo(25*SCREEN_WIDTH_RATIO47);
        }];

    }
    return _coverView;
}

- (void)removeBtnClick:(UIButton *)sender{
    
    
    if ([self.delegate respondsToSelector:@selector(indexBannerSubiew:removeBtnClick:)]) {
        [self.delegate indexBannerSubiew:self removeBtnClick:sender];
    }
    
}

- (void)messageBtnAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(messageBtnAction:)]) {
        [self.delegate messageBtnAction:sender];
    }
}

@end
